package com.api.shopapi.mapper;

import com.api.shopapi.entity.Elderly;
import com.api.shopapi.entity.User;
import com.api.shopapi.entity.dto.UserDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface UserMapper {

    @Insert("insert into user" +
            "(" +
            "userCode, " +
            "loginName, " +
            "phone, " +
            "password, " +
            "name, " +
            "headimg, " +
            "createtime, " +
            "status, " +
            "usertype, " +
            "staffCode, " +
            "institutionCode, " +
            "governmentCode" +
            ") " +
            "value" +
            "(" +
            "#{user.UserCode}," +
            "#{user.loginName}," +
            "#{user.Phone}," +
            "#{user.Password}," +
            "#{user.Name}," +
            "#{user.HeadImg}," +
            "#{user.CreateTime},"+
            "#{user.Status}," +
            "#{user.UserType},"+
            "#{user.StaffCode}," +
            "#{user.institutionCode}," +
            "#{user.GovernmentCode}" +
            ")")
    int insert(@Param("user") User user);

    @Update("update user " +
            " set " +
            " Phone = #{user.Phone}," +
            " loginName = #{user.loginName}," +
            " Password = #{user.Password}," +
            " Name = #{user.Name},"+
            " HeadImg = #{user.HeadImg}," +
            " Status = #{user.Status}," +
            " UserType = #{user.UserType},"+
            " institutionCode = #{user.institutionCode},"+
            " GovernmentCode = #{user.GovernmentCode} " +
            " where " +
            " UserCode=#{user.UserCode}")
    int update(@Param("user") User user);


    @Select("select * from user where UserCode=#{userCode} limit 1")
    User getByUserCode(@Param("userCode") String userCode);

    @Select("select * from user where UserCode=#{userCode} limit 1")
    UserDTO getByForUserDTO(@Param("userCode") String userCode);

    @Select("select * from user where phone=#{phone} limit 1")
    User getByPhone(@Param("phone") String phone);

    @Select("select * from user where loginName=#{loginName} limit 1")
    User getByLoginName(@Param("loginName") String loginName);



    @Select("select * from elderly where IdCard=#{idCard} limit 1")
    Elderly getByIdCard(@Param("idCard") String idCard);

    @Insert("<script>" +
            "insert into user " +
            "(" +
            "UserCode," +
            "Phone," +
            "Password," +
            "Name," +
            "Status," +
            "UserType," +
            "loginName," +
            "GovernmentCode," +
            "institutionCode," +
            "CreateTime" +
            ") " +
            "values " +
            "<foreach collection='list' item='obj' separator=','>" +
            "(" +
            "#{obj.UserCode}," +
            "#{obj.Phone}," +
            "#{obj.Password}," +
            "#{obj.Name}," +
            "#{obj.Status}," +
            "#{obj.UserType}," +
            "#{obj.loginName}," +
            "#{obj.GovernmentCode}," +
            "#{obj.institutionCode}," +
            "#{obj.CreateTime}" +
            ")" +
            "</foreach>" +
            "</script>")
    Integer batchInsert(List<User> list);

    @Select("<script>" +
            "select Phone from user  where Phone in" +
            "<foreach collection='list' item='item' open='(' separator=',' close=')' >" +
            "#{item.Phone}"+
            "</foreach>" +
            "</script>")
    List<String> batchSelectPhone(List<Elderly> list);

    @Select("select * from user where phone=#{phone} and password=#{password} limit 1")
    User getByPhoneAndPassword(@Param("phone") String phone,@Param("password") String password);

    @Select("select * from user where loginName=#{loginName} and password=#{password} limit 1")
    User getByLoginNameAndPassword(@Param("loginName") String loginName,@Param("password") String password);

    @Select("<script>" +
            "select  * from user where 1=1" +
            "<if test =' governmentCode !=\"00000000-0000-0000-0000-000000000000\" '>" +
            " and GovernmentCode=#{governmentCode}" +
            "</if>" +
            "<if test ='name !=null and name!=\"\" '>" +
            " and Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='phone !=null and  phone!=\"\" '>" +
            " and Phone like concat('%',#{phone},'%') " +
            "</if>" +
            "</script>")
    List<UserDTO> getListForAdmin(@Param("governmentCode") String governmentCode,
                                  @Param("name") String name,
                                  @Param("phone") String phone);


    @Select("<script>" +
            "select  * from user where GovernmentCode=#{governmentCode}" +
            "<if test ='name !=null and name!=\"\" '>" +
            " and Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='phone !=null and  phone!=\"\" '>" +
            " and Phone =#{phone} " +
            "</if>" +
            "and status =1 " +
            "</script>")
    List<UserDTO> getListForGov(@Param("governmentCode") String governmentCode,
                                  @Param("name") String name,
                                  @Param("phone") String phone);



    @Select("select * from user")
    List<User> getListAll();


    @Select("<script>" +
            "select * from user where 1=1" +
            "<if test ='name !=null and name!=\"\" '>" +
            " and Name like concat('%',#{name},'%') " +
            "</if>" +
            "<if test ='loginName !=null and loginName!=\"\" '>" +
            " and loginName = #{loginName} " +
            "</if>" +
            "<if test ='phone !=null and  phone!=\"\" '>" +
            " and Phone =#{phone} " +
            "</if>" +
            "</script>")
    List<UserDTO> listByNamePhone(@Param("name") String name,
                                  @Param("phone") String phone,
                                  @Param("loginName") String loginName);

    @Update("<script> update user " +
            " set " +
            " Name = #{user.Name},"+
            " <if test='user.institutionCode!=null'>institutionCode = #{user.institutionCode}</if>" +
            " <if test='user.Phone!=null'>Phone = #{user.Phone}</if>" +
            " <if test='user.loginName!=null'>loginName = #{user.loginName}</if>" +
            " <if test='user.GovernmentCode!=null'>GovernmentCode = #{user.GovernmentCode}</if>" +
            " where " +
            " UserCode=#{user.UserCode}</script>")
    int updateName(User user);
}
